﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using VoiceAI;
using VoiceAI.VoiceResponse;
using Windows.Data.Json;

namespace XiaoweiVoiceAI.ResponseCreators
{
    internal static class VideoResponseCreator
    {
        private class VideoResponseData : IPlayControlResponseData
        {
            public PlayControlType PlayControlType { get; set; }

            public PlayerType PlayerType => PlayerType.Video;

            public IPlayableResource PlayableResource { get; set; }

            public string PlayTittle { get; set; }

            public object PlayStateAdditionalMessage { get; set; }

            public ResponseType ResponseType => ResponseType.PlayControl;

            public ErrorCode ErrorCode => ErrorCode.NoError;

            public string RequestText { get; set; }
            public string ResponseText { get; set; }
            public string TTSUrl { get; set; }

            public bool RestoreInHistory { get; set; }

            public object DataContext { get; set; }

            public int? ControlValue { get; set; }
        }
        private class VideoResource : IVideoResource
        {
            public string DirectorName { get; set; }

            public string ActorName { get; set; }

            public string Year { get; set; }

            public string VideoTag { get; set; }

            public string VideoChannel { get; set; }

            public string PlayName { get; set; }

            public string Season { get; set; }

            public string Episode { get; set; }

            public string Language { get; set; }
        }
        private static Dictionary<XiaoweiV2.CommandId, PlayControlType> commandIdMap = new Dictionary<XiaoweiV2.CommandId, PlayControlType>()
        {
            {XiaoweiV2.CommandId.Play, PlayControlType.Play},
            {XiaoweiV2.CommandId.Stop, PlayControlType.Stop},
            {XiaoweiV2.CommandId.Pause, PlayControlType.Pause},
            {XiaoweiV2.CommandId.Prev, PlayControlType.Prev},
            {XiaoweiV2.CommandId.Next, PlayControlType.Next},
            {XiaoweiV2.CommandId.AddOffset, PlayControlType.AddOffset},
            {XiaoweiV2.CommandId.SetOffsetTo, PlayControlType.SetOffsetTo},
            {XiaoweiV2.CommandId.PlayMode, PlayControlType.PlayMode},
        };
        private static Dictionary<XiaoweiV2.CommandId, string> responseTextMap = new Dictionary<XiaoweiV2.CommandId, string>()
        {
            {XiaoweiV2.CommandId.Play,"好的，开始播放" },
            {XiaoweiV2.CommandId.Stop,"好的，停止播放" },
            {XiaoweiV2.CommandId.Pause,"好的，暂停播放" },
            {XiaoweiV2.CommandId.Prev,"好的，开始播放上一集" },
            {XiaoweiV2.CommandId.Next,"好的，开始播放下一集" },
            {XiaoweiV2.CommandId.AddOffset,"好的，已重新定位" },
            {XiaoweiV2.CommandId.SetOffsetTo,"好的，已重新定位" },
            {XiaoweiV2.CommandId.PlayMode,"好的，已切换播放模式" }
        };

        public static IResponseData CreateResponseData(XiaoweiVoiceAI xiaoweiVoiceAI, XiaoweiV2.ResponseParam param)
        {
            VideoResponseData videoResponseData = new VideoResponseData();
            videoResponseData.RequestText = param.RequestText;
            videoResponseData.ResponseText = param.ResponseText;
            videoResponseData.TTSUrl = XiaoweiV2.ResponseHelper.GetTTSUrl(param);
            videoResponseData.RestoreInHistory = !string.IsNullOrEmpty(param.RequestText);
            if (XiaoweiV2.ResponseHelper.IsCommand(param))
            {
                if (commandIdMap.ContainsKey(param.ControlId))
                {
                    videoResponseData.PlayControlType = commandIdMap[param.ControlId];
                    if (int.TryParse(param.ControlValue, out int value))
                    {
                        if (param.ControlId == XiaoweiV2.CommandId.PlayMode)
                        {
                            switch ((XiaoweiV2.PlayMode)value)
                            {
                                case XiaoweiV2.PlayMode.SequentialNoLoop:
                                    videoResponseData.ControlValue = (int)PlayMode.OrderPlay;
                                    break;
                                case XiaoweiV2.PlayMode.SequentialLoopList:
                                    videoResponseData.ControlValue = (int)PlayMode.LoopPlay;
                                    break;
                                case XiaoweiV2.PlayMode.ShuffleNoLoop:
                                    break;
                                case XiaoweiV2.PlayMode.ShuffleLoop:
                                    videoResponseData.ControlValue = (int)PlayMode.RandomPlay;
                                    break;
                                case XiaoweiV2.PlayMode.SingleLoop:
                                    videoResponseData.ControlValue = (int)PlayMode.SinglePlay;
                                    break;
                                default:
                                    break;
                            }
                        }
                        else
                        {
                            videoResponseData.ControlValue = value / 1000;
                        }
                    }
                    else
                    {
                        videoResponseData.ControlValue = null;
                    }
                    if (!string.IsNullOrEmpty(param.RequestText))
                    {
                        videoResponseData.ResponseText = responseTextMap[param.ControlId];
                        videoResponseData.TTSUrl = xiaoweiVoiceAI.TextToVoice(videoResponseData.ResponseText);
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(param.RequestText))
                {
                    videoResponseData.ResponseText = "已经为您开始播放";
                    videoResponseData.TTSUrl = xiaoweiVoiceAI.TextToVoice("已经为您开始播放");
                }
                videoResponseData.PlayControlType = PlayControlType.PlayResource;
                VideoResource videoResource = new VideoResource();
                //if there is no song list, don't trigger qqlive to play
                JsonObject jsonObject = JsonObject.Parse(param.ResponseData);
                JsonArray jsonArray = jsonObject.GetNamedArray("slot_content");
                Dictionary<string, string> videoData = new Dictionary<string, string>();
                foreach (var item in jsonArray)
                {
                    videoData[item.GetObject().GetNamedString("slot_name")] = item.GetObject().GetNamedString("slot_value");
                }
                try
                {
                    videoResource.DirectorName = videoData["director_name"];
                    videoResource.ActorName = videoData["actor_name"];
                    videoResource.Year = videoData["year"];
                    videoResource.VideoTag = videoData["video_tag"];
                    videoResource.VideoChannel = videoData["video_channel"];
                    videoResource.PlayName = videoData["play_name"];
                    videoResource.Season = videoData["season"];
                    videoResource.Episode = videoData["episode"];
                    videoResource.Language = videoData["language"];
                }
                catch (Exception e)
                {
                    videoResource = new VideoResource();
                }
                videoResponseData.PlayableResource = videoResource;
            }
            return videoResponseData;
        }
    }
}
